module.exports = (app) =>
  class PortalApi {
    getToken() {
      return app.api.clients
        .get('bind')
        .getToken(`auth/wanhuhealth-weixin-portal`);
    }

    setToken(token) {
      return app.api.clients
        .get('bind')
        .setToken(`auth/wanhuhealth-weixin-portal`, token);
    }

    async getPatientInfo(patientId) {
      // 获取会员信息
      return await this.get(`/patient/${patientId}`);
    }

    async getPatientOrder(patientId) {
      return await this.get(`/patient/${patientId}/order`);
    }

    async getHospital(data) {
      return await this.get(`/hospital`, data);
    }

    async getDrugList(hospitalId, data) {
      return await this.get(`/hospital/${hospitalId}/drugs`, data);
    }

    async getDrugDetail(hospitalId, drugId) {
      return await this.get(`/hospital/${hospitalId}/drugs/${drugId}`);
    }

    async postSms(query) {
      return await this.post(`/patient-sms-send`, query);
    }

    async verifyPatient(query) {
      return await this.post(`/patient-sms-verify`, query);
    }

    async getOrderDetail(patientId, orderId) {
      return await this.get(`/patient/${patientId}/order/${orderId}`);
    }

    async getOrderExpressFlowList(orderId) {
      return await this.get(`/orders/${orderId}/expressRouteInfo`);
    }
    async getReturnDetail(orderId) {
      return await this.get(`/orders/${orderId}/billing/reimburse`);
    }

    async getDefaultDrugs(patientId) {
      return await this.get(`/patient/${patientId}/defaultDrugs`);
    }

    async verifyOrder(patientId, data) {
      return await this.get(`/patient/${patientId}/verifyOrder`, data);
    }

    async postOrder(patientId, data) {
      return await this.post(`/patient/${patientId}/order`, data);
    }

    async postOrderNoRule(patientId, data) {
      return await this.post(`/patient/${patientId}/order/norule`, data);
    }

    async getPatientDrugs(patientId, data) {
      return await this.get(`/patient/${patientId}/drugs`, data);
    }

    async getUserInfo() {
      // return await this.get(`/userInfo`)
      const data = {
        name: '周文文',
        id: '10',
        hospitalName: '雨花台区赛虹桥社区卫生服务中心',
        hospitalId: 'a10',
      };
      return data;
    }

    async getDoctors(hospitalId) {
      return await this.get(`/hospital/${hospitalId}/doctors`);
      // const data = [
      //     {
      //         name: "周文文",
      //         id: "10"
      //     },
      //     {
      //         name: "张三",
      //         id: "20"
      //     },
      //     {
      //         name: "李四",
      //         id: "30"
      //     }
      // ]
      // return data
    }

    async getPackages(param) {
      // return await this.get(`/packages`, param)
      let { where } = param;
      where = JSON.parse(where);
      let data = {};
      if (where.doctorId === '10') {
        data = {
          count: 4,
          list: [
            {
              patientName: '王东风',
              patientId: '100',
              sex: 1,
              age: 66,
              phone: '187543512223',
              notice: true,
              status: 1,
            },
            {
              patientName: '韩梅梅',
              patientId: '101',
              sex: 0,
              age: 36,
              phone: '187921512223',
              notice: false,
              status: 0,
            },
            {
              patientName: '杰克',
              patientId: '102',
              sex: 1,
              age: 86,
              phone: '1875438794123',
              notice: true,
              status: 1,
            },
            {
              patientName: '李磊',
              patientId: '103',
              sex: 1,
              age: 16,
              phone: '187543545623',
              notice: false,
              status: 1,
            },
          ],
        };
      } else if (where.doctorId === '20') {
        data = {
          count: 5,
          list: [
            {
              patientName: '王东风2',
              patientId: '110',
              sex: 1,
              age: 56,
              phone: '187548712223',
              notice: true,
              status: 1,
            },
            {
              patientName: '韩梅梅2',
              patientId: '120',
              sex: 0,
              age: 46,
              phone: '187925412223',
              notice: false,
              status: 0,
            },
            {
              patientName: '杰克2',
              patientId: '130',
              sex: 1,
              age: 36,
              phone: '187546456194123',
              notice: true,
              status: 1,
            },
            {
              patientName: '李磊2',
              patientId: '140',
              sex: 1,
              age: 16,
              phone: '187545445623',
              notice: false,
              status: 1,
            },
            {
              patientName: '李狗蛋',
              patientId: '150',
              sex: 1,
              age: 26,
              phone: '187545445623',
              notice: false,
              status: 1,
            },
          ],
        };
      } else if (where.doctorId === '30') {
        data = {
          count: 1,
          list: [
            {
              patientName: '南风',
              patientId: '200',
              sex: 1,
              age: 36,
              phone: '18755452223',
              notice: true,
              status: 1,
            },
          ],
        };
      }
      return data;
    }

    async getPatientDetail(patientId) {
      // return await this.get(`/${patientId}/patientDetail`)
      const data = {
        patientName: '王东风',
        patientId: '100',
        sex: 1,
        age: 66,
        phone: '187543512223',
        signHospital: '雨花台区赛虹桥社区卫生服务中心',
        signDoctor: '周文文',
        insuranceName: '城镇职工',
        diseases: [
          {
            diseaseCode: '010',
            diseaseName: '高血压',
          },
          {
            diseaseCode: '020',
            diseaseName: '心脏病',
          },
        ],
        allergyHistory: '过敏史',
        list: [
          {
            takeDate: '2018-2-22',
            status: 1,
            drugs: [
              {
                drugName: '阿卡波糖片',
                drugId: '5454',
                commonName: '拜糖平',
                amount: 2,
              },
              {
                drugName: '硝苯地平缓释片',
                drugId: '5468',
                commonName: '拜新同',
                amount: 5,
              },
            ],
          },
          {
            takeDate: '2018-2-26',
            status: 0,
            drugs: [
              {
                productName: '阿卡波糖片',
                drugId: '5454',
                commonName: '拜糖平',
                amount: 4,
              },
              {
                productName: '硝苯地平缓释片',
                drugId: '5468',
                commonName: '拜新同',
                amount: 1,
              },
              {
                productName: '阿莫西林',
                drugId: '4587',
                commonName: '消炎药',
                amount: 5,
              },
            ],
          },
        ],
      };
      return data;
    }

    async getOrderCancelReason() {
      return await this.get(`/cfg/enum/orderCancelReason`);
    }

    async getOrderLogs(orderId) {
      return this.get(`/orders/${orderId}/logs`);
    }

    async getOrders(param) {
      return await this.get(`/orders`, param);
    }

    async cancelReason(orderId, data) {
      return await this.post(`/orders/${orderId}/cancel`, data);
    }

    async activeMobileVerify(mobilePhone, activityId) {
      return await this.get(`/hd/mobiles/${mobilePhone}/verify`, {
        activityId,
      });
    }

    //配置类型：1=福利计划，2=联系我们，3=模版1，4=模版2 ，5=素材id
    async getConfigurations(weixinLabel, configType, patientId) {
      const configList = await this.get(
        `/weixin/${weixinLabel}/configurations`,
        { patientId }
      );
      if (configList && configType) {
        const config = configList.find(
          (config) => config.configType == configType
        );
        return config ? config.content : null;
      }
      return configList;
    }
    async isBinding(patientId, weixinLabel) {
      return await this.get(`/patients/${patientId}/isBinding`, {
        weixinLabel,
      });
    }

    async getPatientsAvailable(param) {
      return await this.get(`/patients/available`, param);
    }

    async userSendCode(param) {
      return await this.post(`/userSendCode`, param);
    }

    async getPatientNewInfo(param) {
      let newparam = '';
      if (param) {
        for (let i in param) {
          newparam += param[i];
        }
      }
      return await this.get(`/lxyl/${newparam}/statistic`);
    }

    async getInsurances() {
      return await this.get(`/cfg/enum/insurances`);
    }

    async getDiseases() {
      return await this.get(`/cfg/enum/diseases`);
    }

    async getRegions(param) {
      return await this.get(`/cfg/enum/regions`, param);
    }

    async openInfo() {
      return await this.get(`/openInfo`);
    }

    // 找药-列表
    async getNewDrugList(param) {
      return await this.get(`/drugs`, param);
    }

    // 找药-社区医院目录
    async getHosDrugDetail(param) {
      return await this.get(
        `/hospital/${param.hospitalId}/drugs/${param.drugId}`
      );
    }

    // 找药-基础目录
    async getBasisDrugDetail(param) {
      return await this.get(`/drugs/${param.baseDrugId}`);
    }

    // 找药-查询需求单
    async queryDemandList(param) {
      return await this.get(`/patients/${param.patientId}/requisitionForm`);
    }

    // 找药-添加药品至需求单
    async addDrugToDemand(param) {
      return await this.post(
        `/patients/${param.patientId}/requisitionForm`,
        param
      );
    }

    // 找药-添加药品至需求单
    async updateDrugToDemand(patientId, formId, param) {
      return await this.put(
        `/patients/${patientId}/requisitionForm/${formId}`,
        param
      );
    }

    // 获取绿A开通信息
    async getAopenInfo() {
      return await this.get(`/openInfo`);
    }

    async uploadImages(param) {
      return await this.post('/uploadImages', param);
    }

    async getPoint(patientId) {
      return await this.get(`/patient/${patientId}/rewardpoints`);
    }

    async getPointRecord(patientId, query) {
      return await this.get(`/patient/${patientId}/rewardpoints/record`, query);
    }

    async receiverAddress(patientId, query) {
      return await this.get(`/patients/${patientId}/receiverAddress`, query);
    }

    async addReceiverAddress(patientId, body) {
      return await this.post(`/patients/${patientId}/receiverAddress`, body);
    }

    async updateReceiverAddress(patientId, id, body) {
      return await this.put(
        `/patients/${patientId}/receiverAddress/${id}`,
        body
      );
    }

    async deleteReceiverAddress(patientId, id) {
      return await this.delete(`/patients/${patientId}/receiverAddress/${id}`);
    }

    async getNewOrderList(patientId, data) {
      return await this.get(`/patient/${patientId}/order`, data);
    }

    // 获取一键续订
    async queryOrderCommonList(patientId) {
      return await this.get(`/patients/${patientId}/routineDrugs`);
    }

    async getOrder(res1, res2) {
      return await this.get(`/patient/${res1.patientId}/order`, res2);
    }
    async rewardpoints(patientId) {
      return await this.get(`/patient/${patientId}/rewardpoints`);
    }

    async welfare(patientId, param) {
      return await this.post(`/patients/${patientId}/welfare`, param);
    }
    async defaultStoreAddress(patientId) {
      return await this.get(`/patients/${patientId}/defaultStoreAddress`);
    }
    async updateDefaultStoreAddress(patientId, body) {
      return await this.post(
        `/patients/${patientId}/defaultStoreAddress`,
        body
      );
    }
    async getPatientFreight(patientId, body) {
      return await this.get(`/patient/${patientId}/freight`, body);
    }

    async getInsuranceProducts(insurance_package_id) {
      return await this.get(
        `/insurance_packages/${insurance_package_id}/products`
      );
    }
    // 获取保障列表
    async getServiceArticleList(packageId) {
      return await this.get(`/insurance_package/${packageId}/specifications`);
    }
    async getInsuranceDiseaseInquiries(insurance_package_id) {
      return await this.get(
        `/insurance_packages/${insurance_package_id}/disease_inquiries`
      );
    }

    async creatInsuranceOrder(body) {
      return await this.post(`/patients/insurance_orders`, body);
    }  
    
    

    async payInsuranceOrder(patientId, insuranceOrderId, memeberId, body) {
      return await this.post(
        `/patients/${patientId}/insurance_orders/${insuranceOrderId}/pay?memberId=${memeberId}`,
        body
      );
    }
    async idCardCheck(query) {
      return await this.get(`/idCardCheck`, query);
    }
    async getWarehouseDrugs(warehouseId, query) {
      return await this.get(`/warehouses/${warehouseId}/drugs`, query);
    }

    async getInsuranceOrderList(patientId, query) {
      return await this.get(`/patients/${patientId}/insuranceOrder`, query);
    }

    async getInsuranceOrderInfo(orderId) {
      return await this.get(`/patients/insurance_order/${orderId}`);
    }

    async getInsurancePayInfo(
      insuranceOrderId,
      insuranceOrderBillId,
      patientId,
      payOrderId
    ) {
      return await this.get(
        `/patients/${patientId}/insurance_orders/${insuranceOrderId}/insurance_order_bills/${insuranceOrderBillId}/payment_info/${payOrderId}`
      );
    }

    async getBodyMassList(patientId, where) {
      return await this.get(`/patients/${patientId}/bodyMass`, where);
    }

    async subBodyMass(data) {
      let { where } = data;
      where = JSON.parse(where);
      return await this.post(`/patients/${data.patientId}/bodyMass`, where);
    }

    async getShareCode() {
      const data = 'wanhubao';
      return await this.get(`/activityConfig/${data}`);
    }

    async getShareNum(patientId, activityId) {
      return await this.post(`/patients/${patientId}/forwarding/${activityId}`);
    }

    async updatePatientCertification(patientId, body) {
      return await this.post(
        `/patients/${patientId}/patientCertification`,
        body
      );
    }

    async verifyPatientInsuranceOrder(body) {
      return await this.get(
        `/insurance_order/verifyPatientInsuranceOrder`,
        body
      );
    }

    async getCityList() {
      return await this.get(`/cfg/provinceAndCity`);
    }

    async checkHospital(data) {
      return await this.post(`/patients/${data.id}/hospital`, {
        provinceId: data.provinceId,
        cityId: data.cityId,
      });
    }

    async postNotificationForInsurance(patientId, insuranceOrderId, param) {
      return await this.post(
        `/patients/${patientId}/insuranceOrders/${insuranceOrderId}/notification`,
        param
      );
    }

    async getInusranceImgList(insuranceId) {
      return await this.get(``);
    }

    async getBaseDrug(param) {
      return await this.get(`/baseDrugs`, param);
    }

    async getMedicationEvaluationList(patientId) {
      return await this.get(`/patients/${patientId}/medicationEvaluation`);
    }

    async medicationEvaluation(param) {
      return await this.get(
        `/patients/${param.patientId}/medicationEvaluation/${param.evaluationId}`
      );
    }

    async getPatientInsuranceService(patientId) {
      return await this.get(`/patient/${patientId}/insurance_order/inservice`);
    }

    async getArticles(where, skip, limit, nearBy) {
      return await this.get(`/wcm/articles`, { where, skip, limit, nearBy });
    }

    async getArticleDetail(articleId) {
      // console.log("##############################################",articleId)
      return await this.get(`/wcm/articles/${articleId}`);
    }
    async getHealthDiseases() {
      return await this.get(`/hdDiseases`);
    }

    async getOrderCount(packageId) {
      return await this.get(`/insurance_packages/${packageId}/count`);
    }

    async getAdpositionsAds(query) {
      return await this.get(`/wcm/adpositions/ads`, query);
    }
    async getArticles(query) {
      return await this.get(`/wcm/articles`, query);
    }
    async getTodayOptimization(query) {
      return await this.get(`/wcm/todayOptimization`, query);
    }
    async getAdpositions(positionName) {
      return await this.get(`/wcm/adpositions/ads`, { positionName });
    }

    async getArticleCategorise(query) {
      return await this.get(`/wcm/articleCategorise`, query);
    }

    async getCodeImg(query) {
      return await this.get(`/patients/${query}/wxacode`);
    }
    async checkForkCode(query) {
      return await this.get('/insurance_order/fork_code', query);
    }
    async getCodeStatus(query) {
      return await this.get(`/patients/${query}/wxa`);
    }
    async mergePatient(query) {
      return await this.post(`/patients/merge`, query);
    }
    async getHistories(id) {
      const data = await this.get(`/insurance_order/insurer/${id}/histories`);
      try {
        if (data) {
          return data;
        } else {
          return [];
        }
      } catch (error) {
        return [];
      }
    }
    async getHomeInsurancelist(query) {
      return await this.get(`/insurance_packages`, query);
    }
    async getInsuranceTeamOrder(id) {
      return await this.get(`/patient/${id}/insurance_team_order/inservice`);
    }
    async getinsuranceCode(query) {
      return await this.put(
        `/insurance_packages/${query.id}/activationCode?userId=${query.userId}`
      );
    }
    async edituserInfo(
      id,
      estimatedPickupPeriod,
      estimatedPickupDay,
      estimatedDoctorId,
      estimatedHospitalId
    ) {
      return await this.put(
        `/patient/${id}/regularMedicationInfo?estimatedPickupPeriod=${estimatedPickupPeriod}&estimatedPickupDay=${estimatedPickupDay}&estimatedDoctorId=${estimatedDoctorId}&estimatedHospitalId=${estimatedHospitalId}`
      );
    }
    async doctorList(patientId) {
      try {
        const data = await this.get(`/patients/${patientId}/wxaRecords`);
        if (data) {
          return data;
        } else {
          return [];
        }
      } catch (error) {
        return [];
      }
    }
    async mecService(patientId) {
      try {
        const data = await this.get(`/patients/${patientId}/mecService`);
        if (data) {
          return data;
        } else {
          return [];
        }
      } catch (error) {
        return [];
      }
    }
    async mecServicebind(id, patientId) {
      const data = await this.post(
        `/hd/medicalExaminationCards/binding?productId=${id}&patientId=${patientId}`
      );
      return data;
    }
    async mecServicesend(id) {
      const data = await this.post(
        `/hd/medicalExaminationCards/send?productId=${id}`
      );
      return data;
    }
    async getInquiry(id) {
      return await this.get(`/insurance_package/${id}/inquiries`)
    }
    // 查询直赔余额
    async queryEnterpriseRemainAmount(patientId) {
      console.log('查询直赔余额')
      const data = await this.get(
        `/patient/${patientId}/enterprisewelfare/remainamount`
      );
      console.log('queryEnterpriseRemainAmount data:',data)
      return data;
    }
  }
